﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agile.Manager.Util;
using Agile.Manager.SQLite;
using System.Windows.Input;
using Agile.Manager.Utilities.UI;
using Agile.Manager.Windows.Model;
using Agile.Manager.Hub;
using System.Windows.Controls;

namespace Agile.Manager.UI.Windows.ViewModel
{
    public class TaskDetailInfoViewModel : ObservableObject
    {
        SQLiteDataManagement sql = new SQLiteDataManagement();

        public TaskDetailInfoViewModel()
        {

        }

        public ICommand SaveTaskDetail
        {
            get
            {
                return new DelegateCommand<EventArgs>((e) =>
                {
                    try
                    {
                        sql.Save<Task>(SelectedTask);
                        Error = "Task Saved";
                        MessageForeColor = "Green";
                    }
                    catch (Exception)
                    {
                        Error = "Error Saving data";
                        MessageForeColor = "Red";
                    }
                }
                );
            }
        }

        public ICommand ProgressTextChanged
        {
            get
            {
                return new DelegateCommand<EventArgs>((e) =>
                {
                    TextBox oa = ((System.Windows.RoutedEventArgs)(e)).OriginalSource as TextBox;
                    
                    SaveText("Progress", oa);
                });
            }
        }

        public ICommand ResourcesTextChanged
        {
            get
            {
                return new DelegateCommand<EventArgs>((e) =>
                {
                    TextBox oa = ((System.Windows.RoutedEventArgs)(e)).OriginalSource as TextBox;

                    SaveText("Resources", oa);
                });
            }
        }

        public ICommand ResponsibleTextChanged
        {
            get
            {
                return new DelegateCommand<EventArgs>((e) =>
                {
                    TextBox oa = ((System.Windows.RoutedEventArgs)(e)).OriginalSource as TextBox;

                    SaveText("Responsible", oa);
                });
            }
        }

        public ICommand DurationTextChanged
        {
            get
            {
                return new DelegateCommand<EventArgs>((e) =>
                {
                    TextBox oa = ((System.Windows.RoutedEventArgs)(e)).OriginalSource as TextBox;

                    SaveText("Duration", oa);
                });
            }
        }

        private void SaveText(string property, TextBox oa)
        {
            switch (property)
            {
                case "Progress":
                    SelectedTask.Progress = oa.Text;
                    break;
                case "Resources":
                    SelectedTask.Resources = oa.Text;
                    break;
                case "Duration":
                    SelectedTask.Duration = oa.Text;
                    break;
                case "Responsible":
                    SelectedTask.Responsible = oa.Text;
                    break;
                default:
                    break;
            }
        }

        public Task SelectedTask 
        { 
            get
            {
                return (TaskHub.SelectedTask == null) ? new Task() : TaskHub.SelectedTask;
            } 

            set
            {
                TaskHub.SelectedTask = value;
                RaisePropertyChanged("SelectedTask");
            } 
        }

        string _forecolor;
        public string MessageForeColor
        {
            get { return _forecolor; }
            private set
            {
                _forecolor = value;
                RaisePropertyChanged("MessageForeColor");
            }
        }

        string _error;
        public string Error
        {
            get
            {
                return _error;
            }
            private set
            {
                _error = value;
                RaisePropertyChanged("Error");
            }
        }
    }
}
